package com.example.bankmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bankmanage.dto.rep.GetTransferRecordsReqDTO;
import com.example.bankmanage.entity.PageBean;
import com.example.bankmanage.entity.transactions;
import com.example.bankmanage.mapper.TransactionMapper;
import com.example.bankmanage.service.TransactionService;
import org.springframework.stereotype.Service;

@Service
public class TransactionServiceimpl extends ServiceImpl<TransactionMapper, transactions> implements TransactionService {

    @Override
    public PageBean<transactions> getTransferRecords(GetTransferRecordsReqDTO requestParam) {
        // 构建分页参数
        Page<transactions> page = new Page<>(requestParam.getPageNum(), requestParam.getPageSize());

        // 构建查询条件
        LambdaQueryWrapper<transactions> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        queryWrapper.eq(requestParam.getTransactionId() != null, transactions::getTransactionId, requestParam.getTransactionId())
                .eq(requestParam.getReferenceNumber() != null && requestParam.getReferenceNumber() != "", transactions::getReferenceNumber, requestParam.getReferenceNumber())
                .eq(requestParam.getTransactionType() != null, transactions::getTransactionType, requestParam.getTransactionType())
                .eq(requestParam.getFromAccountId() != null, transactions::getFromAccountId,requestParam.getFromAccountId())
                .eq(requestParam.getToAccountId() != null,transactions::getToAccountId,requestParam.getToAccountId())
                .ge(requestParam.getStartDate() != null, transactions::getTransactionDate, requestParam.getStartDate())
                .le(requestParam.getEndDate() != null, transactions::getTransactionDate, requestParam.getEndDate());

        // 添加排序条件
        if (StringUtils.isNotBlank(requestParam.getOrderByColumn()) && StringUtils.isNotBlank(requestParam.getOrderByColumn())) {
            queryWrapper.orderBy(true, "asc".equalsIgnoreCase(requestParam.getIsAsc()),
                    getSortField(requestParam.getOrderByColumn()));
        } else {
            // 默认按账单ID降序
            queryWrapper.orderByDesc(transactions::getTransactionId);
        }
        page = baseMapper.selectPage(page, queryWrapper);
//        System.out.println(page.getPages());
//        System.out.println(page.getTotal());
        //这里的mybatis-plus里的count指令没有执行,要调
        PageBean<transactions> pageBean = new PageBean<>();
        pageBean.setContent(page.getRecords());
        pageBean.setTotalElements(page.getTotal());
        // 执行查询
        return pageBean;
    }
    // 根据前端传入的sortField转换为实体字段
    private SFunction<transactions, ?> getSortField(String sortField) {
        switch (sortField) {
            case "transactionId": return transactions::getTransactionId;
            case "referenceNumber": return transactions::getReferenceNumber;
            case "transactionType": return transactions::getTransactionType;
            case "status": return transactions::getStatus;
//            case "paymentDate": return bills::getPaymentDate;
            default: return transactions::getTransactionId;
        }
    }
}
